package _Pan3D.core
{
	import _Pan3D.base.Camera3D;
	import _Pan3D.base.Focus3D;
	import flash.geom.Vector3D;

	// --------------MSN:lation_pan@live.cn  QQ: 3423526------------- //
	public class MathCore
	{
		public static function getSourcePro(vSourceXml:XML):Array
		{
			// 将XML转为数组对象
			var tempArray:Array=new Array;
			for (var i:int=0; i < vSourceXml.children().length(); i++)
			{
				var z:Object=new Object;
				for (var j:int=0; j < vSourceXml.child(i).children().length(); j++)
				{
					var vT:String=String(vSourceXml.child(i).child(j).name());
					z[vT]=vSourceXml.child(i).child(j);
				}
				tempArray[i]=z;
			}
			return tempArray;
		}

		public static function argbToHex(a:uint, r:uint, g:uint, b:uint):uint
		{
			// 转换颜色
			var color:uint=a << 24 | r << 16 | g << 8 | b;
			return color;
		}

		public static function _catch_cam(_Cam:Camera3D, _focus_3d:Focus3D):void
		{
			// 根据捆定点，及距离算出镜头的坐标及角度。

			var view_angle_x:Number=_focus_3d.angle_x;
			var view_angle_y:Number=_focus_3d.angle_y;
			var rx:Number=0;
			var ry:Number=0;
			var rz:Number=-_Cam.distance;

			var sin_y:Number=Math.sin(view_angle_y * Math.PI / 180);
			var cos_y:Number=Math.cos(view_angle_y * Math.PI / 180);
			var sin_x:Number=Math.sin(view_angle_x * Math.PI / 180);
			var cos_x:Number=Math.cos(view_angle_x * Math.PI / 180);


			//	var tmp_rx = this.rx;
			//	this.rx = int(Math.cos(tmp_angle)*tmp_rx-Math.sin(tmp_angle)*this.rz);
			//	this.rz = int(Math.sin(tmp_angle)*tmp_rx+Math.cos(tmp_angle)*this.rz);

			var tmp_rz:Number=rz;
			rz=cos_x * tmp_rz - sin_x * ry;
			ry=sin_x * tmp_rz + cos_x * ry;

			var tmp_rx:Number=rx;
			rx=cos_y * tmp_rx - sin_y * rz;
			rz=sin_y * tmp_rx + cos_y * rz;

			_Cam.x=rx + _focus_3d.x;
			_Cam.y=ry + _focus_3d.y;
			_Cam.z=rz + _focus_3d.z;

			_Cam.angle_x=_focus_3d.angle_x;
			_Cam.angle_y=_Cam.angle_y + (_focus_3d.angle_y - _Cam.angle_y) / 1;

			_Cam.sin_y=Math.sin((_Cam.angle_y) * Math.PI / 180);
			_Cam.cos_y=Math.cos((_Cam.angle_y) * Math.PI / 180);

			_Cam.sin_x=Math.sin((_Cam.angle_x) * Math.PI / 180);
			_Cam.cos_x=Math.cos((_Cam.angle_x) * Math.PI / 180);

			_Cam.cameraMatrix.identity();
			_Cam.cameraMatrix.prependScale(1, _Cam.fovw / _Cam.fovh, 1);
			_Cam.cameraMatrix.prependTranslation(0, 0, _Cam.distance);
			_Cam.cameraMatrix.prependRotation(_Cam.angle_x, Vector3D.X_AXIS);
			_Cam.cameraMatrix.prependRotation(_Cam.angle_y, Vector3D.Y_AXIS);
			_Cam.cameraMatrix.prependTranslation(-_focus_3d.x, -_focus_3d.y, -_focus_3d.z);
		}


	}
}
